package com.ruoyi.project.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.domain.InsMachineReagentCode;
import com.ruoyi.project.product.domain.InsMachineReagentCodeDTO;
import com.ruoyi.project.product.service.IInsMachineReagentCodeService;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 机型试剂代码Controller
 *
 * @author taney
 * @date 2020-12-15
 */
@AllArgsConstructor
@RestController
@RequestMapping("/product/machineReagentCode")
public class InsMachineReagentCodeController extends BaseController {

    private final IInsMachineReagentCodeService iInsMachineReagentCodeService;

    /**
     * 查询机型试剂代码列表
     */
    @PreAuthorize("@ss.hasPermi('product:machineReagentCode:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsMachineReagentCode insMachineReagentCode) {
        startPage();
        LambdaQueryWrapper<InsMachineReagentCode> lqw = new LambdaQueryWrapper<InsMachineReagentCode>();
        if (StringUtils.isNotBlank(insMachineReagentCode.getMachineType())) {
            lqw.eq(InsMachineReagentCode::getMachineType, insMachineReagentCode.getMachineType());
        }
        if (StringUtils.isNotBlank(insMachineReagentCode.getReagentType())) {
            lqw.eq(InsMachineReagentCode::getReagentType, insMachineReagentCode.getReagentType());
        }
        if (StringUtils.isNotBlank(insMachineReagentCode.getCodeNo())) {
            lqw.eq(InsMachineReagentCode::getCodeNo, insMachineReagentCode.getCodeNo());
        }
        List<InsMachineReagentCodeDTO> list = iInsMachineReagentCodeService.listMachineReagentCodeDTO(lqw);
        list.forEach(machineReagentCodeDTO -> {
            LambdaQueryWrapper<InsMachineReagentCode> lqw2 = new LambdaQueryWrapper<InsMachineReagentCode>();
            lqw2.eq(InsMachineReagentCode::getMachineType, machineReagentCodeDTO.getMachineType());
            List<InsMachineReagentCode> machineReagentCodeList = iInsMachineReagentCodeService.list(lqw2);
            machineReagentCodeDTO.setMachineReagentCodeList(machineReagentCodeList);
        });
        return getDataTable(list);
    }

    /**
     * 导出机型试剂代码列表
     */
    @PreAuthorize("@ss.hasPermi('product:machineReagentCode:export')")
    @Log(title = "机型试剂代码", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsMachineReagentCode insMachineReagentCode) {
        LambdaQueryWrapper<InsMachineReagentCode> lqw = new LambdaQueryWrapper<InsMachineReagentCode>(insMachineReagentCode);
        List<InsMachineReagentCode> list = iInsMachineReagentCodeService.list(lqw);
        ExcelUtil<InsMachineReagentCode> util = new ExcelUtil<InsMachineReagentCode>(InsMachineReagentCode.class);
        return util.exportExcel(list, "machineReagentCode");
    }

    /**
     * 获取机型试剂代码详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:machineReagentCode:query')")
    @GetMapping(value = "/{machineType}")
    public AjaxResult getInfo(@PathVariable("machineType") String machineType) {
        LambdaQueryWrapper<InsMachineReagentCode> lqw = new LambdaQueryWrapper<>();
        lqw.eq(InsMachineReagentCode::getMachineType, machineType);
        List<InsMachineReagentCode> list = iInsMachineReagentCodeService.list(lqw);
        InsMachineReagentCodeDTO machineReagentCodeDTO = new InsMachineReagentCodeDTO();
        BeanUtils.copyProperties(list.get(0), machineReagentCodeDTO);
        machineReagentCodeDTO.setMachineReagentCodeList(list);
        return AjaxResult.success(machineReagentCodeDTO);
    }

    /**
     * 新增机型试剂代码
     */
    @PreAuthorize("@ss.hasPermi('product:machineReagentCode:add')")
    @Log(title = "机型试剂代码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsMachineReagentCode insMachineReagentCode) {
        return toAjax(iInsMachineReagentCodeService.save(insMachineReagentCode) ? 1 : 0);
    }

    /**
     * 批量新增机型试剂代码
     */
    @PreAuthorize("@ss.hasPermi('product:machineReagentCode:addBatch')")
    @Log(title = "批量新增机型试剂代码", businessType = BusinessType.INSERT)
    @PostMapping("/addBatch")
    public AjaxResult addBatch(@RequestBody ObjInsMachineReagentCodeList objList) {
        return toAjax(iInsMachineReagentCodeService.saveBatch(objList.getList()) ? 1 : 0);
    }

    /**
     * 修改机型试剂代码
     */
    @PreAuthorize("@ss.hasPermi('product:machineReagentCode:edit')")
    @Log(title = "机型试剂代码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsMachineReagentCodeDTO machineReagentCodeDTO) {
        List<InsMachineReagentCode> machineReagentCodeList = machineReagentCodeDTO.getMachineReagentCodeList();
        machineReagentCodeList.forEach(one -> {
            if (one.getMachineReagentCodeId() != null) {
                iInsMachineReagentCodeService.updateById(one);
            } else {
                iInsMachineReagentCodeService.save(one);
            }
        });
        return AjaxResult.success();
    }

    /**
     * 删除机型试剂代码
     */
    @PreAuthorize("@ss.hasPermi('product:machineReagentCode:remove')")
    @Log(title = "机型试剂代码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{machineReagentCodeIds}")
    public AjaxResult remove(@PathVariable Integer[] machineReagentCodeIds) {
        return toAjax(iInsMachineReagentCodeService.removeByIds(Arrays.asList(machineReagentCodeIds)) ? 1 : 0);
    }
}

@Data
class ObjInsMachineReagentCodeList {
    List<InsMachineReagentCode> list;
}